package com.vnas.fonan;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

import com.vnas.database.FonanDatabase;
import com.vnas.dialog.FonanDialog;

public class LoginActivity extends Activity {

	private static final String PREFS_NAME = "loginactivity";
	Button btnOk, btnCancel;
	EditText etuser, etpass;
	CheckBox chkrememberuser;
	FonanDatabase db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initViews();
		setTitle("Login to Fonan");

	}

	public void initViews() {
		db = new FonanDatabase(this);
		try {
			db.createDatabase();
			db.openDatabase();
		} catch (IOException e) {
			e.printStackTrace();
		}

		final SharedPreferences s = getSharedPreferences(PREFS_NAME,
				MODE_PRIVATE);

		etuser = (EditText) findViewById(R.id.et_username);
		etpass = (EditText) findViewById(R.id.et_pass);
		btnOk = (Button) findViewById(R.id.btn_login_ok);
		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (checkLogin(etuser.getText().toString(), etpass.getText()
						.toString())) {
					Intent i = new Intent(getApplicationContext(),
							MainActivity.class);
					i.putExtra("username", etuser.getText().toString());
					
					startActivity(i);
					LoginActivity.this.finish();
				} else {
					FonanDialog.showMessage(LoginActivity.this, "Error", "Login failed. "
						+ "Please check your username and password again.");
				}

			}
		});

		btnCancel = (Button) findViewById(R.id.btn_login_cancel);
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				db.close();
				LoginActivity.this.finish();

			}
		});

		chkrememberuser = (CheckBox) findViewById(R.id.chk_remember_username);

		boolean isRemember = s.getBoolean("isremember", true);
		if (isRemember) {
			etuser.setText(s.getString("curusername", ""));
			
		}
		chkrememberuser.setChecked(isRemember);

		chkrememberuser
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {

						SharedPreferences.Editor ed = s.edit();
						ed.putBoolean("isremember", isChecked);
						ed.putString("curusername", etuser.getText().toString());

						ed.commit();

					}
				});
	}

	public boolean checkLogin(String un, String pass) {

		String sql = "select name, pass from " + FonanDatabase.FONAN_DATABASE_TABLE_ACCOUNT
				+ " where name = '" + un + "' and pass = '" + pass + "'";

		SQLiteDatabase sdb = db.getSQLiteDatabase();
		Cursor cursor = sdb.rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			cursor.close();
			return true;
		}
		cursor.close();
		return false;
	}
}
